package com.scpii.api.common.base;

import java.io.Serializable;

import org.codehaus.jackson.map.annotate.JsonSerialize;

import com.scpii.api.common.Constants;
import com.scpii.api.common.exception.ClientException;
import com.scpii.api.common.json.serializer.ResponseSerializer;

@JsonSerialize(using = ResponseSerializer.class)
public class Response implements Serializable {

	/**
	 * 
	 */
	private static final long serialVersionUID = -6970857721862781111L;

	public enum Status {

		SUCCESS(2000, "request success"),

		IMPROVE_USER_INFO(2001, "improve the user information"),

		INVALID_REQUEST(1000, "invalid request"),

		INVALID_CLIENT(1001, "invalid client"),

		INVALID_USER(1002, "invalid user"),

		INVALID_TOKEN(1003, "invalid token"),

		CLINET_DENIED(1004, "client access denied "),

		USER_DENIED(1005, "user access denied"),

		UNAUTHORIZED(1006, "unauthorized"),

		UNSUPPPORT_GRANT_TYPE(1007, "unsupported grant type"),

		SERVER_ERROR(1008, "server error"),

		INVALID_VAlID_CODE(1009, "invalid valid code"),

		INVALID_PAGE(1010, "invalid page"),

		INVALID_METHOD(1011, "method not support"),

		DOT_NOT_ALLOW_REGISTER(1012, "does not allow user registration"),

		USER_ID_DISACCORD(1013, "userId is not disaccord"),

		IOS_TOKEN_FORMAT_ERROR(1014, "ios token format error"),

		EC_ERROR(1015, "ec error"),

		POINTS_BALANCE_NOT_ENOUGH(1016, "points balance not enough");

		private final Integer value;

		private final String description;

		private Status(Integer value, String description) {
			this.value = value;
			this.description = description;
		}

		public static Status valueOf(int statusCode) {
			for (Status status : values()) {
				if (status.value == statusCode) {
					return status;
				}
			}
			throw new IllegalArgumentException("No matching constant for ["
					+ statusCode + "]");
		}

		public Integer getValue() {
			return value;
		}

		public String getDescription() {
			return description;
		}

		@Override
		public String toString() {
			return String.valueOf(value);
		}

	}

	private Integer count = 0;

	private Integer startIndex = 0;

	private Integer nextIndex = 0;

	private String tips;

	private Status status = Status.SUCCESS;

	private String statusDescription = Status.SUCCESS.getDescription();

	private Object response;

	public Response() {
		super();
	}

	public Response(Object response) {
		this.response = response;
	}

	public Response(ClientException e) {
		exception(e);
	}

	public Response(ClientException e, String tips) {
		this(e);
		this.tips = tips;
	}

	public Response(Status status) {
		this.status = status;
		this.statusDescription = status.getDescription();
	}

	public Response(Status status, String tips) {
		this(status);
		this.tips = tips;
	}

	public Response(String tips) {
		this.tips = tips;
	}

	public Integer getCount() {
		return count;
	}

	public void setCount(Integer count) {
		this.count = count;
	}

	public void setNextIndex(Integer nextIndex) {
		this.nextIndex = nextIndex;
	}

	public Status getStatus() {
		return status;
	}

	public void setStatus(Status status) {
		this.status = status;
		this.statusDescription = status.getDescription();
	}

	public String getStatusDescription() {
		return statusDescription;
	}

	public void setStatusDescription(String statusDescription) {
		this.statusDescription = statusDescription;
	}

	public Object getResponse() {
		return response;
	}

	public void setResponse(Object response) {
		this.response = response;
	}

	public Integer getNextIndex() {
		return nextIndex;
	}

	public Integer getStartIndex() {
		return startIndex;
	}

	public void setStartIndex(Integer startIndex) {
		this.startIndex = startIndex;
	}

	public String getTips() {
		return tips;
	}

	public void setTips(String tips) {
		this.tips = tips;
	}

	public void exception(ClientException e) {
		this.status = e.getStatus();
		if (Constants.IS_DEBUG) {
			this.statusDescription = e.getMessage() == null ? e.getStatus()
					.getDescription() : e.getMessage();
		} else {
			this.statusDescription = e.getStatus().getDescription();
		}
	}

}
